import { ref, computed } from 'vue';
import { defineStore } from 'pinia';

export const useCounterStore = defineStore('counter', () => {
  const count = ref(0);
  const doubleCount = computed(() => count.value * 2);
  function increment() {
    count.value++;
  }
  //创建一个自定义范围的年份数组
  function createYearRange(startYear, endYear) {
    const years = [];
    for (let year = startYear; year <= endYear; year++) {
      years.push(year);
    }
    return years;
  }

  const yearRange = createYearRange(2000, 2030);
  const monthArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
  //获取当前年份
  const year = ref(new Date().getFullYear());
  //获取当前月份
  const month = ref(new Date().getMonth());
  const trueMonth = ref(month.value + 1);

  const selectedYear = ref(year.value);
  const selectedMonth = ref(trueMonth.value);

  function setSelectedYear(year) {
    selectedYear.value = year;
  }

  function setSelectedMonth(month) {
    selectedMonth.value = month;
  }

  function setSelectedMonthAdd() {
    if (selectedMonth.value === 12) {
      selectedYear.value++;
      selectedMonth.value = 1;
    } else {
      selectedMonth.value++;
    }
  }
  function setSelectedMonthSub() {
    if (selectedMonth.value === 1) {
      selectedYear.value--;
      selectedMonth.value = 12;
    } else {
      selectedMonth.value--;
    }
  }
  return { count, doubleCount, increment, yearRange, monthArr, year, month, selectedYear, selectedMonth, setSelectedYear, setSelectedMonth, setSelectedMonthAdd, setSelectedMonthSub };
});
